﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary.BLL;
using CommonLibrary.Model;
using SendMsg.SmsSeries.ServiceSmsSeries;

namespace SmsSeries
{
    public partial class frmSendMsg : Form
    {
        #region 变量

        SmsSeriesSoapClient smsSeriesSoapClient = new SmsSeriesSoapClient();

        List<RadioButton> rbPrioritys = new List<RadioButton>();

        /// <summary>
        /// TabPage
        /// </summary>
        private enum TabPageEnum
        {
            /// <summary>
            /// 发送
            /// </summary>
            Send,
            /// <summary>
            /// 联系人
            /// </summary>
            Linkman,
            /// <summary>
            /// 联系人分组
            /// </summary>
            LinkmanGroup
        }

        /// <summary>
        /// 是否是更新
        /// </summary>
        private bool isUpdate;

        /// <summary>
        /// 联系人
        /// </summary>
        frmLinkman FrmLinkman=new frmLinkman();

        /// <summary>
        /// 联系人分组
        /// </summary>
        frmLinkmanGroup FrmLinkmanGroup=new frmLinkmanGroup();

        /// <summary>
        /// 短信模板
        /// </summary>
        frmTemplate FrmTemplate=new frmTemplate();

        private TemplateInfo TemplateSelectedItem;

        #endregion

        #region 属性

        /// <summary>
        /// 使能菜单
        /// </summary>
        private bool EnableToolBar
        {
            set
            {
                tsmiDeleteTreeNode.Enabled = value;
                tsmiUpdateTreeNode.Enabled = value;
                tsmiAddLinkmanTreeNode.Enabled = value;

            }
        }

        private bool EnableTemplateToolBar
        {
            set
            {
                tsmiUpdateTemplate.Enabled = value;
                tsmiDeleteTemplate.Enabled = value;
            }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        private int UserID { get; set; }

        #endregion

        #region 控件事件

        //构造函数
        public frmSendMsg(int userID)
        {
            UserID = userID;
            InitializeComponent();
        }

        //发送
        private void btnSend_Click(object sender, EventArgs e)
        {
            int priority = 0;
            for (int i = 0; i < rbPrioritys.Count; i++)
            {
                if (rbPrioritys[i].Checked)
                {
                    priority = Convert.ToInt32(rbPrioritys[i].Tag);
                }
            }

            UserInfoInfo userInfoInfo = (UserInfoInfo) cbUserID.SelectedItem;

            smsSeriesSoapClient.SendMsg(userInfoInfo.UserName.ToString(), tbDestNumber.Text,
                tbContent.Text, tbSignName.Text, priority, (int)nuValidMinute.Value);

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        //窗体加载
        private void frmSendMsg_Load(object sender, EventArgs e)
        {
            rbPrioritys.Add(rbPriority1);
            rbPrioritys.Add(rbPriority2);
            rbPrioritys.Add(rbPriority3);

            //SelectTabPage(TabPageEnum.Send);

            LoadTreeNodes();

            LoadTemplates(0);

            EnableToolBar = false;
            EnableTemplateToolBar = false;

            //加载UserID
            List<UserInfoInfo> userInfoInfos = UserInfo.GetList("");
            foreach (UserInfoInfo userInfoInfo in userInfoInfos)
            {
                if (userInfoInfo.UserID== UserID)
                {
                    cbUserID.Items.Add(userInfoInfo);
                }
            }
            if (cbUserID.Items.Count > 0)
            {
                cbUserID.SelectedIndex = 0;
            }

            cbUserID.Enabled = false;

            //加载事件
            FrmLinkman.AddOrUpdateLinkmanEvent += new EventHandler<CommonLibrary.Data.TwoEventArgs>(FrmLinkman_AddOrUpdateLinkmanEvent);
            FrmLinkmanGroup.AddOrUpdateLinkmanGroupEvent += new EventHandler<CommonLibrary.Data.OneEventArgs>(FrmLinkmanGroup_AddOrUpdateLinkmanGroupEvent);
            FrmTemplate.AddOrUpdateTemplateEvent += new EventHandler<CommonLibrary.Data.TwoEventArgs>(FrmTemplate_AddOrUpdateTemplateEvent);

            FrmLinkman.initializeComponent();
            FrmLinkmanGroup.initializeComponent();
            FrmTemplate.initializeComponent();
        }

        //添加或修改短信模板
        void FrmTemplate_AddOrUpdateTemplateEvent(object sender, CommonLibrary.Data.TwoEventArgs e)
        {
            if (!isUpdate)
            {
                TemplateInfo templateInfo = new TemplateInfo(UserID, e.Obj1.ToString(), e.Obj2.ToString());
                Template.Insert(templateInfo);
                //tbGroupName.Text = "";
                //List<TemplateInfo> templateInfos =
                //    Template.GetList("TempID = (select max(TempID) from Template)");

                LoadTemplates(lbTemplateList.SelectedIndex);
            }
            else
            {
                TemplateInfo templateInfo = new TemplateInfo(
                   UserID, e.Obj1.ToString(), e.Obj2.ToString());
                templateInfo.TempID = ((TemplateInfo) lbTemplateList.SelectedItem).TempID;
                Template.Update(templateInfo);
                //tbGroupName.Text = "";

                LoadTemplates(lbTemplateList.SelectedIndex);
            }

            FrmTemplate.Close();
        }

        //添加或修改联系人分组
        void FrmLinkmanGroup_AddOrUpdateLinkmanGroupEvent(object sender, CommonLibrary.Data.OneEventArgs e)
        {
            if (!isUpdate)
            {
                Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(e.Obj1.ToString());
                Linkman_Group.Insert(linkman_GroupInfo);
                //tbGroupName.Text = "";
                List<Linkman_GroupInfo> linkman_GroupInfos =
                    Linkman_Group.GetList("linkmanGroupNo = (select max(linkmanGroupNo) from linkman_Group)");
                TreeNode node = new TreeNode();
                node.Name = linkman_GroupInfos[0].LinkmanGroupNo.ToString();
                node.Text = linkman_GroupInfos[0].LinkmanGroupName;
                node.Tag = linkman_GroupInfos[0];
                tvLinkman.Nodes.Add(node);
            }
            else
            {
                Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(
                    Convert.ToInt32(tvLinkman.SelectedNode.Name), e.Obj1.ToString());
                Linkman_Group.Update(linkman_GroupInfo);
                //tbGroupName.Text = "";

                tvLinkman.SelectedNode.Text = linkman_GroupInfo.LinkmanGroupName;
                tvLinkman.SelectedNode.Tag = linkman_GroupInfo;
            }

            FrmLinkmanGroup.Close();
        }

        //添加或修改联系人
        void FrmLinkman_AddOrUpdateLinkmanEvent(object sender, CommonLibrary.Data.TwoEventArgs e)
        {
            if (!isUpdate)
            {
                LinkmanInfo linkmanInfo = new LinkmanInfo(e.Obj1.ToString(), e.Obj2.ToString());
                Linkman.Insert(linkmanInfo);

                //取到最大的
                List<LinkmanInfo> linkmanInfos = Linkman.GetList("Linkmanid = (select max(Linkmanid) from Linkman)");

                //关联
                LinkmanGroupInfo linkmanGroupInfo = new LinkmanGroupInfo(Convert.ToInt32(
                    tvLinkman.SelectedNode.Parent == null ? tvLinkman.SelectedNode.Name : tvLinkman.SelectedNode.Parent.Name
                    ), linkmanInfos[0].LinkmanID);
                LinkmanGroup.Insert(linkmanGroupInfo);

                if (tvLinkman.SelectedNode.Parent == null)
                {
                    TreeNode node = new TreeNode();
                    node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Name,
                                              linkmanInfos[0].LinkmanID.ToString());
                    node.Text = linkmanInfos[0].LinkmanName;
                    node.Tag = linkmanInfos[0];
                    tvLinkman.SelectedNode.Nodes.Add(node);
                }
                else
                {
                    TreeNode node = new TreeNode();
                    node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Parent.Name,
                                              linkmanInfos[0].LinkmanID.ToString());
                    node.Text = linkmanInfos[0].LinkmanName;
                    node.Tag = linkmanInfos[0];
                    tvLinkman.SelectedNode.Parent.Nodes.Add(node);
                }
            }
            else
            {
                char[] spliter = { '-' };
                string[] strings = tvLinkman.SelectedNode.Name.Split(spliter);
                LinkmanInfo linkmanInfo = new LinkmanInfo(Convert.ToInt32(strings[1]), e.Obj1.ToString(), e.Obj2.ToString());
                Linkman.Update(linkmanInfo);

                tvLinkman.SelectedNode.Text = linkmanInfo.LinkmanName;
                tvLinkman.SelectedNode.Tag = linkmanInfo;
            }

            FrmLinkman.Close();
        }

        //TreeNodes 上单击鼠标左键
        private void tvLinkman_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tvLinkman.SelectedNode = e.Node;
                EnableToolBar = true;
            }
        }

        //TreeNodes 上双击鼠标
        private void tvLinkman_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Node.Parent == null)
            {
                //组
                foreach (TreeNode node in e.Node.Nodes)
                {
                    //联系人
                    char[] spliter = { '-' };
                    string[] strings = node.Name.Split(spliter);
                    List<LinkmanInfo> linkmanInfos = Linkman.GetList(string.Format("LinkmanID = {0}", strings[1]));
                    
                    if (tbDestNumber.Text == "")
                    {
                        tbDestNumber.Text = linkmanInfos[0].LinkmanTel;
                    }
                    else
                    {
                        if (!tbDestNumber.Text.Contains(linkmanInfos[0].LinkmanTel))
                        {
                            tbDestNumber.AppendText(string.Format(";{0}", linkmanInfos[0].LinkmanTel));
                        }
                    }
                }
            }
            else
            {
                //联系人
                char[] spliter = {'-'};
                string[] strings = e.Node.Name.Split(spliter);
                List<LinkmanInfo> linkmanInfos = Linkman.GetList(string.Format("LinkmanID = {0}", strings[1]));
                if (tbDestNumber.Text == "")
                {
                    tbDestNumber.Text = linkmanInfos[0].LinkmanTel;
                }
                else
                {
                    if (!tbDestNumber.Text.Contains(linkmanInfos[0].LinkmanTel))
                    {
                        tbDestNumber.AppendText(string.Format(";{0}", linkmanInfos[0].LinkmanTel));
                    }
                }
            }
        }

        //TreeNode 菜单关闭前
        private void cmsTreeNodes_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            EnableToolBar = false;
        }

        //菜单 添加联系人
        private void tsmiAddLinkmanTreeNode_Click(object sender, EventArgs e)
        {
            //SelectTabPage(TabPageEnum.Linkman);
            //todo:联系人变动
            //btnOKLinkman.Text = "添加";
            FrmLinkman.IsUpdate = false;
            isUpdate = false;
            FrmLinkman.ShowDialog(this);

            //tbLinkmanName.Text = "";
            //tbLinkmanTel.Text = "";
        }

        //菜单添加联系人分组
        private void tsmiAddGroupTreeNode_Click(object sender, EventArgs e)
        {
            //SelectTabPage(TabPageEnum.LinkmanGroup);

            //todo:分组变动
            //btnOKGroup.Text = "添加";
            FrmLinkmanGroup.IsUpdate = false;
            isUpdate = false;
            FrmLinkmanGroup.ShowDialog(this);
            
            //tbGroupName.Text = "";
        }

        //菜单 更新
        private void tsmiUpdateTreeNode_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = tvLinkman.SelectedNode;

            if (selectedNode.Parent == null)
            {
                //SelectTabPage(TabPageEnum.LinkmanGroup);

                //todo:分组变动
                //btnOKGroup.Text = "修改";
                isUpdate = true;
                FrmLinkmanGroup.IsUpdate = true;
                FrmLinkmanGroup.LinkmanGroupName = selectedNode.Text;
                FrmLinkmanGroup.ShowDialog(this);

                //tbGroupName.Text = selectedNode.Text;
            }
            else
            {
                //SelectTabPage(TabPageEnum.Linkman);
                //todo:联系人变动
                //btnOKLinkman.Text = "修改";
                isUpdate = true;

                LinkmanInfo linkmanInfo = (LinkmanInfo)selectedNode.Tag;
                FrmLinkman.LinkmanName = linkmanInfo.LinkmanName;
                FrmLinkman.LinkmanTel = linkmanInfo.LinkmanTel;
                FrmLinkman.IsUpdate = true;
                FrmLinkman.ShowDialog(this);
                //tbLinkmanName.Text = linkmanInfo.LinkmanName;
                //tbLinkmanTel.Text = linkmanInfo.LinkmanTel;
            }
        }

        //菜单 删除
        private void tsmiDeleteTreeNode_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = tvLinkman.SelectedNode;

            if (selectedNode.Parent == null)
            {
                if (MessageBox.Show(string.Format("请确认是否要删除联系人组‘{0}’？",selectedNode.Text), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //删除联系人
                    foreach (TreeNode node in selectedNode.Nodes)
                    {
                        char[] spliter1 = { '-' };
                        string[] strings1 = node.Name.Split(spliter1);

                        Linkman.Delete(Convert.ToInt32(strings1[1]));
                    }

                    //删除联系人组
                    Linkman_Group.Delete(Convert.ToInt32(selectedNode.Name));

                    //删除关联
                    LinkmanGroup.DeleteByLinkmanGroupNo(Convert.ToInt32(selectedNode.Name));

                    tvLinkman.Nodes.Remove(tvLinkman.SelectedNode);
                }
            }
            else
            {
                if (MessageBox.Show(string.Format("请确认是否要删除联系人‘{0}’？",selectedNode.Text), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //删除联系人
                    char[] spliter = { '-'};
                    string[] strings = selectedNode.Name.Split(spliter);

                    Linkman.Delete(Convert.ToInt32(strings[1]));

                    //删除关联
                    LinkmanGroup.DeleteByLinkmanID(Convert.ToInt32(strings[1]));

                    tvLinkman.Nodes.Remove(tvLinkman.SelectedNode);
                }
            }
        }

        //菜单 添加 短信模板
        private void tsmiAddTemplate_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            FrmTemplate.IsUpdate = false;
            FrmTemplate.ShowDialog(this);
        }

        //菜单 修改 短信模板
        private void tsmiUpdateTemplate_Click(object sender, EventArgs e)
        {
            //FrmTemplate.TemplateName =
            isUpdate = true;
            FrmTemplate.TemplateName = ((TemplateInfo)lbTemplateList.SelectedItem).TempName;
            FrmTemplate.TemplateContent = ((TemplateInfo)lbTemplateList.SelectedItem).TempContent;

            FrmTemplate.IsUpdate = true;
            FrmTemplate.ShowDialog(this);
        }

        //菜单 删除 短信模板
        private void tsmiDeleteTemplate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(string.Format("请确认是否要删除短信模板‘{0}’？", lbTemplateList.SelectedItem.ToString()), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Template.Delete(((TemplateInfo) lbTemplateList.SelectedItem).TempID);
                LoadTemplates(lbTemplateList.SelectedIndex - 1 < 0 ? 0 : lbTemplateList.SelectedIndex - 1);
            }
        }

        //短信模板列表 选择改变
        private void lbTemplateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbTemplateList.SelectedItem != null)
            {
                TemplateSelectedItem = (TemplateInfo)lbTemplateList.SelectedItem;
                tbTemplateContent.Text = ((TemplateInfo)lbTemplateList.SelectedItem).TempContent;
            }
            else
            {
                TemplateSelectedItem = null;
                lbTemplateList.SelectedIndex = -1;
            }
            
        }

        //短信模板双击
        private void lbTemplateList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lbTemplateList.SelectedItem != null)
            {
                tbContent.Text = ((TemplateInfo)lbTemplateList.SelectedItem).TempContent;
            }
        }

        ////联系人 添加修改
        //private void btnOKLinkman_Click(object sender, EventArgs e)
        //{
        //    if (!isUpdate)
        //    {
        //        LinkmanInfo linkmanInfo=new LinkmanInfo(tbLinkmanName.Text,tbLinkmanTel.Text);
        //        Linkman.Insert(linkmanInfo);

        //        //取到最大的
        //        List<LinkmanInfo> linkmanInfos = Linkman.GetList("Linkmanid = (select max(Linkmanid) from Linkman)");

        //        //关联
        //        LinkmanGroupInfo linkmanGroupInfo = new LinkmanGroupInfo(Convert.ToInt32(
        //            tvLinkman.SelectedNode.Parent ==null ? tvLinkman.SelectedNode.Name : tvLinkman.SelectedNode.Parent.Name
        //            ), linkmanInfos[0].LinkmanID);
        //        LinkmanGroup.Insert(linkmanGroupInfo);

        //        if (tvLinkman.SelectedNode.Parent == null)
        //        {
        //            TreeNode node=new TreeNode();
        //            node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Name,
        //                                      linkmanInfos[0].LinkmanID.ToString());
        //            node.Text = linkmanInfos[0].LinkmanName;
        //            node.Tag = linkmanInfos[0];
        //            tvLinkman.SelectedNode.Nodes.Add(node);
                    
        //        }
        //        else
        //        {
        //            TreeNode node=new TreeNode();
        //            node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Parent.Name,
        //                                      linkmanInfos[0].LinkmanID.ToString());
        //            node.Text = linkmanInfos[0].LinkmanName;
        //            node.Tag = linkmanInfos[0];
        //            tvLinkman.SelectedNode.Parent.Nodes.Add(node);
        //        }
        //    }
        //    else
        //    {    
        //        char[] spliter = { '-' };
        //        string[] strings =   tvLinkman.SelectedNode.Name.Split(spliter);
        //        LinkmanInfo linkmanInfo = new LinkmanInfo(Convert.ToInt32(strings[1]), tbLinkmanName.Text, tbLinkmanTel.Text);
        //        Linkman.Update(linkmanInfo);

        //        tvLinkman.SelectedNode.Text = linkmanInfo.LinkmanName;
        //        tvLinkman.SelectedNode.Tag = linkmanInfo;
        //    }
        //}

        ////联系人 取消
        //private void btnCancelLinkman_Click(object sender, EventArgs e)
        //{
        //    //SelectTabPage(TabPageEnum.Send);
        //}

        ////联系人分组 添加修改
        //private void btnOKGroup_Click(object sender, EventArgs e)
        //{
        //    if(!isUpdate)
        //    {
        //        Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(tbGroupName.Text);
        //        Linkman_Group.Insert(linkman_GroupInfo);
        //        tbGroupName.Text = "";
        //        List<Linkman_GroupInfo> linkman_GroupInfos =
        //            Linkman_Group.GetList("linkmanGroupNo = (select max(linkmanGroupNo) from linkman_Group)");
        //        TreeNode node=new TreeNode();
        //        node.Name = linkman_GroupInfos[0].LinkmanGroupNo.ToString();
        //        node.Text = linkman_GroupInfos[0].LinkmanGroupName;
        //        node.Tag = linkman_GroupInfos[0];
        //        tvLinkman.Nodes.Add(node);
        //    }
        //    else
        //    {
        //        Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(
        //            Convert.ToInt32(tvLinkman.SelectedNode.Name), tbGroupName.Text);
        //        Linkman_Group.Update(linkman_GroupInfo);
        //        tbGroupName.Text = "";

        //        tvLinkman.SelectedNode.Text = linkman_GroupInfo.LinkmanGroupName;
        //        tvLinkman.SelectedNode.Tag = linkman_GroupInfo;
        //    }
        //}

        ////联系人分组 取消
        //private void btnCancelGroup_Click(object sender, EventArgs e)
        //{
        //    SelectTabPage(TabPageEnum.Send);
        //}

        private void cmsTemplates_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            EnableTemplateToolBar = false;
        }

        private void lbTemplateList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //lbTemplateList.Items[0].
                EnableTemplateToolBar = true;
            }
        }

        #endregion

        #region 方法

        ///// <summary>
        ///// 选择TabPage
        ///// </summary>
        ///// <param name="tabPageEnum">tabPage</param>
        //private void SelectTabPage(TabPageEnum tabPageEnum)
        //{
        //    switch (tabPageEnum)
        //    {
        //        case TabPageEnum.Send:

        //            pSend.Visible = true;
        //            pLinkman.Visible = false;
        //            pGroup.Visible = false;
        //            pSend.Dock = DockStyle.Fill;

        //            break;
        //        case TabPageEnum.Linkman:

        //            pSend.Visible = false;
        //            pLinkman.Visible = true;
        //            pGroup.Visible = false;
        //            pLinkman.Dock = DockStyle.Fill;

        //            break;
        //        case TabPageEnum.LinkmanGroup:

        //            pSend.Visible = false;
        //            pLinkman.Visible = false;
        //            pGroup.Visible = true;
        //            pGroup.Dock = DockStyle.Fill;

        //            break;
        //        default:
        //            break;
        //    }
        //}

        /// <summary>
        /// 加载树
        /// </summary>
        private void LoadTreeNodes()
        {
            tvLinkman.Nodes.Clear();

            List<Linkman_GroupInfo> linkman_GroupInfos = Linkman_Group.GetList("");

            if (linkman_GroupInfos.Count > 0)
            {
                foreach (Linkman_GroupInfo linkman_GroupInfo in linkman_GroupInfos)
                {
                    TreeNode node = new TreeNode();
                    node.Name = linkman_GroupInfo.LinkmanGroupNo.ToString();
                    node.Text = linkman_GroupInfo.LinkmanGroupName;
                    node.Tag = linkman_GroupInfo;
                    tvLinkman.Nodes.Add(node);

                    List<LinkmanGroupInfo> linkmanGroupInfos = LinkmanGroup.GetList(string.Format(" LinkmanGroupNO = {0}",node.Name) );
                    foreach (LinkmanGroupInfo linkmanGroupInfo in linkmanGroupInfos)
                    {
                        List<LinkmanInfo> linkmanInfos =
                            Linkman.GetList(string.Format(" LinkmanID = {0}", linkmanGroupInfo.LinkmanID));
                        if(linkmanInfos.Count > 0)
                        {
                            TreeNode node1 = new TreeNode();
                            node1.Name = string.Format("{0}-{1}", linkman_GroupInfo.LinkmanGroupNo, linkmanInfos[0].LinkmanID);
                            node1.Text = linkmanInfos[0].LinkmanName;
                            node1.Tag = linkmanInfos[0];
                            node.Nodes.Add(node1);
                        }
                    }
                }
                tvLinkman.ExpandAll();
            }
        }

        /// <summary>
        /// 加载短信模板
        /// </summary>
        private void LoadTemplates(int selectIndex)
        {
            lbTemplateList.Items.Clear();

            List<TemplateInfo> templateInfos = Template.GetList("userid = "+UserID);

            if (templateInfos.Count > 0)
            {
                foreach (TemplateInfo templateInfo in templateInfos)
                {
                    lbTemplateList.Items.Add(templateInfo);
                }
                lbTemplateList.SelectedIndex = selectIndex;
            }
        }

        #endregion
    }
}
